package com.pospi.xstore.ui.static_ui;

import android.app.DatePickerDialog;
import android.os.Bundle;
import android.text.format.DateFormat;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.ListView;
import android.widget.TextView;

import com.pospi.xstore.R;
import com.pospi.xstore.adapter.CashierStaticAdapter;
import com.pospi.xstore.dao.OrderDao;
import com.pospi.xstore.dto.CashierCashDto;
import com.pospi.xstore.dto.OrderDto;
import com.pospi.xstore.ui.base.BaseActivity;
import com.pospi.xstore.util.GetData;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class CashierStaticActivity extends BaseActivity {

    @Bind(R.id.lv_goods)
    ListView lvGoods;
    @Bind(R.id.sale_ranking_data)
    TextView saleRankingData;
    @Bind(R.id.sale_ranking_data_to)
    TextView saleRankingDataTo;
    @Bind(R.id.search)
    Button search;
    private int fromData;
    private int toData;

    private CashierStaticAdapter adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentLayout(R.layout.activity_cashier_static);
        setTitle("收银员统计");
        ButterKnife.bind(this);
        saleRankingData.setText(GetData.getYYMMDDTime());
        fromData = GetData.getSearchTime();

        saleRankingDataTo.setText(GetData.getYYMMDDTime());
        toData = GetData.getSearchTime();

        getOrderDate();
    }


    public void showPickerDialog(final TextView tv, final int num) {
        final Calendar c1 = Calendar.getInstance();
        DatePickerDialog dialog1 = new DatePickerDialog(CashierStaticActivity.this, new DatePickerDialog.OnDateSetListener() {
            @Override
            public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
                c1.set(year, monthOfYear, dayOfMonth);
                tv.setText(DateFormat.format("yyyy-MM-dd", c1));
                switch (num) {
                    case SaleRankingActivity.FROM:
                        fromData = Integer.parseInt(DateFormat.format("yyyyMMdd", c1).toString());
                        break;
                    case SaleRankingActivity.TO:
                        toData = Integer.parseInt(DateFormat.format("yyyyMMdd", c1).toString());
                        break;
                }
            }
        }, c1.get(Calendar.YEAR), c1.get(Calendar.MONTH), c1.get(Calendar.DAY_OF_MONTH));
        dialog1.show();
    }


    public void getOrderDate() {
        List<OrderDto> dtos = new OrderDao(getApplicationContext()).selectGoodsBetween(fromData, toData);
        Log.d(dtos.size() + "数据大小", "内容");

        /**
         * 把订单的list转化成收银员的订单信息的list
         */
        List<CashierCashDto> cashierCashDtos = new ArrayList<>();
        for (int i = 0; i < dtos.size(); i++) {
            CashierCashDto cashDto = new CashierCashDto();
            cashDto.setNum(1);
            cashDto.setCashierName(dtos.get(i).getCashiername());
            cashDto.setCashierNumber(dtos.get(i).getCashiernumber());
            cashDto.setDiscount(0);
            cashDto.setGap(dtos.get(i).getYs_money());
            cashDto.setIn(dtos.get(i).getYs_money());
            cashDto.setSale(dtos.get(i).getYs_money());
            cashierCashDtos.add(cashDto);
        }

        /**
         * 把同一个收银员所处理的订单累加在一起
         */
        Log.i("size", cashierCashDtos.size() + "");
        for (int i = 0; i < cashierCashDtos.size(); i++) {
            CashierCashDto good1 = cashierCashDtos.get(i);
            String good_name1 = good1.getCashierName();
            for (int j = cashierCashDtos.size() - 1; j > i; j--) {
                CashierCashDto good2 = cashierCashDtos.get(j);
                String good_name2 = good2.getCashierName();
                if (good_name1.equals(good_name2)) {
                    good1.setNum(good1.getNum() + good2.getNum());
                    good1.setSale(good1.getSale() + good2.getSale());
                    good1.setIn(good1.getIn() + good2.getIn());
                    good1.setGap(good1.getGap() + good2.getGap());
                    good1.setDiscount(good1.getDiscount() + good2.getDiscount());
                    good1.setPayment(good1.getPayment() + good2.getPayment());
                    cashierCashDtos.remove(good2);
                }
            }
        }

        Collections.sort(cashierCashDtos, new Comparator<CashierCashDto>() {
            @Override
            public int compare(CashierCashDto user1, CashierCashDto user2) {
                Integer id1 = user1.getNum();
                Integer id2 = user2.getNum();
                //可以按User对象的其他属性排序，只要属性支持compareTo方法
                return id2.compareTo(id1);
            }
        });

        adapter = new CashierStaticAdapter(getApplicationContext(), cashierCashDtos);
        lvGoods.setAdapter(adapter);
    }

    @OnClick({R.id.sale_ranking_data, R.id.sale_ranking_data_to, R.id.search})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.sale_ranking_data:
                showPickerDialog(saleRankingData, SaleRankingActivity.FROM);
                break;
            case R.id.sale_ranking_data_to:
                showPickerDialog(saleRankingDataTo, SaleRankingActivity.TO);
                break;
            case R.id.search:
                if (toData >= fromData & toData <= GetData.getSearchTime()) {
                    getOrderDate();
                } else {
                    showToast("请选择正确的查询日期");
                }
                break;
        }
    }
}
